﻿using System;
using System.Collections.Generic;
using Educational.Model.Entity;
using NHibernate;
using NHibernate.Criterion;

namespace Educational.Model.EntityManager
{
    public class SchoolManager
    {
        public static void Add(ISession session, School an)
        {
            session.Save(an);
        }

        public static void Delete(ISession session, School an)
        {
            session.Delete(an);
        }

        public static void Update(ISession session, School an)
        {
            session.Update(an);
        }

        public static School Get(ISession session, int itemId)
        {
            return session.Get<School>(itemId);
        }

        /// <summary>
        /// 根据登录ID获取一个School对象
        /// </summary>
        /// <param name="session"></param>
        /// <param name="schoolLoginId"></param>
        /// <returns></returns>
        public static School Get1(ISession session, string loginId)
        {
            ICriteria c = session.CreateCriteria(typeof(School));
            c.Add(Expression.Eq("loginId", loginId));
            return c.UniqueResult<School>();
        }


        public static School Get2(ISession session, string loginId, string loginPass)
        {
            ICriteria c = session.CreateCriteria(typeof(School));
            c.Add(Expression.Eq("loginId", loginId));
            c.Add(Expression.Eq("loginPass", loginPass));
            c.SetFetchMode("schoolType", FetchMode.Select);
            return c.UniqueResult<School>();
        }

        public static IList<School> Gets1(ISession session, string schoolName, string loginId, int rowIndex, int rowCount)
        {
            ICriteria c = session.CreateCriteria(typeof(School));
            if(!String.IsNullOrEmpty(schoolName))
                c.Add(Expression.Like("schoolName", "%" + schoolName + "%"));
            if(!String.IsNullOrEmpty(loginId))
                c.Add(Expression.Eq("loginId", loginId));
            c.Add(Expression.Not(Expression.Eq("status", 1)));
            c.SetFetchMode("schoolType", FetchMode.Eager);
            c.SetFirstResult(rowIndex);
            c.SetMaxResults(rowCount);
            c.AddOrder(Order.Desc("schoolId"));
            return c.List<School>();
        }
        public static int Gets1Count(ISession session, string schoolName, string loginId)
        {
            ICriteria c = session.CreateCriteria(typeof(School));
            if (!String.IsNullOrEmpty(schoolName))
                c.Add(Expression.Like("schoolName", "%" + schoolName + "%"));
            if (!String.IsNullOrEmpty(loginId))
                c.Add(Expression.Eq("loginId", loginId));
            c.Add(Expression.Not(Expression.Eq("status", 1)));
            return (int)c.SetProjection(Projections.Count("schoolId")).UniqueResult();
        }
    }
}
